package com.project.lk.nurseapp.nurse.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.project.lk.nurseapp.base.BaseApi;
import com.project.lk.nurseapp.base.PostBaseModel;
import com.project.lk.nurseapp.home.usermsg.model.PostFileHintBean;
import com.project.lk.nurseapp.login.model.HintBean;
import com.project.lk.nurseapp.utils.JsonUtils;
import com.project.lk.nurseapp.utils.SharedPreferencesUtils;
import com.project.lk.nurseapp.utils.ToastUtils;
import com.project.lk.nurseapp.utils.UserInfoUtils;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class SendMsgModel extends PostBaseModel {

    private String mType;

    public void postLoadJson(String url, String sessionId,String content, String msgType){
        mType = msgType;
        Log.e("MLOG",msgType+">>>>>>>>>>>>>>");
        Random random = new Random();
        int nextInt = random.nextInt();
        FormBody.Builder builder = new FormBody.Builder();
        long currentTimeMillis = System.currentTimeMillis();
        builder.add("signature",postSignToken(currentTimeMillis+"",nextInt+""))
                .add("timestamp",currentTimeMillis+"")
                .add("nonce",nextInt+"")
                .add("accountId",new UserInfoUtils().getAccountId())
                .add("sessionId",sessionId)
                .add("username",new UserInfoUtils().getUsername())
                .add("content",content)
                .add("senderType","0") //发送人帐户类型 0普通用户  1护士 2医生
                .add("msgType",msgType);
        getJsonData(url,builder.build());
    }
    @Override
    public List<HintBean> analyzeJson(String json, String url) {
        Log.e("MLOG",url+json);
        try {
            JSONObject jsonObject=new JSONObject(json);
            String info = jsonObject.getString("info");
            String msg = jsonObject.getString("msg");
            if (TextUtils.equals(msg,"success")) {
                switch (mType){
                    case BaseApi.image_type:
                            postImgSuccess();
                        break;
                    case BaseApi.audio_type:
                            postAudioSuccess();
                        break;
                    case BaseApi.video_type:
                            postVideoSuccess();
                        break;
                    case BaseApi.text_type:
                        postTextSuccess();
                        break;
                }
            }else {
                ToastUtils.showShortToast(info);
            }
            List<HintBean> list=new ArrayList<>();
            HintBean bean = (HintBean) JsonUtils.getJsonUtils()
                    .toObject(new TypeToken<HintBean>(){}.getType(),json.toString());
            list.add(bean);
            ToastUtils.showShortToast(info);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("MLOG",e.getMessage());
            return null;
        }
    }


    public void postImgSuccess(){
        bridge.bridge(BaseApi.send_img_success,null);
    }
    public void postImgFail(){
        bridge.bridge(BaseApi.send_img_fail,null);
    }

    public void postVideoSuccess(){
        bridge.bridge(BaseApi.send_video_success,null);
    }
    public void postVideoFail(){
        bridge.bridge(BaseApi.send_video_fail,null);
    }

    public void postAudioSuccess(){
        bridge.bridge(BaseApi.send_audio_success,null);
    }
    public void postAudioFail(){
        bridge.bridge(BaseApi.send_audio_fail,null);
    }

    public void postTextSuccess(){
        bridge.bridge(BaseApi.send_text_success,null);
    }
    public void postTextFail(){
        bridge.bridge(BaseApi.send_text_fail,null);
    }

    List<HintBean> mList=new ArrayList<>();
   public List<HintBean> getHintList(){
        return mList;
    }
    @Override
    public void postSuccessOnMainThread(List list, String url) {
        if (list!=null) {
            mList.clear();
            mList.addAll(list);
            if (TextUtils.equals(mList.get(0).getMsg(),"fail")) {
                switch (mType){
                    case BaseApi.image_type:
                        postImgFail();
                        break;
                    case BaseApi.audio_type:
                        postAudioFail();
                        break;
                    case BaseApi.video_type:
                        postVideoFail();
                        break;
                    case BaseApi.text_type:
                        postTextFail();
                        break;
                }
            }
        }
    }

    @Override
    public void postFailedOnMainThread(String url) {
        switch (mType){
            case BaseApi.image_type:
                postImgFail();
                break;
            case BaseApi.audio_type:
                postAudioFail();
                break;
            case BaseApi.video_type:
                postVideoFail();
                break;
            case BaseApi.text_type:
                postTextFail();
                break;
        }
    }
}
